package com.xiaozhichao.javaapi.base.io;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;
import java.util.Objects;

public class TxtFileExecute {

    @Test
    public void write(){
        FileWriter writer = null;
        String filePath = "D:\\demo.sql";
        try {
            File file = new File(filePath);
            file.delete();
            file.createNewFile();
            writer = new FileWriter(file,true);
            writer.write("我写入一条\n");
            writer.write("我写入一条\n");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(Objects.nonNull(writer)){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Test
    public void demo(){
        InputStream inputStream = null;
        BufferedReader br = null;
        try {
            inputStream = new FileInputStream("C:\\Users\\xiaozhichao\\Desktop\\work\\temp\\额度助贷还款文件\\risk_loan_actual_repay_detail20200831.txt");
            br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line;

            while((line = br.readLine()) != null){
                DemoEntry entry = new DemoEntry();
                sb.append(line);
                sb.append("\n");
                String[] strArray = line.split("\\|");
                entry.setStr(strArray[2]);
                System.out.println(entry.toString());
            }
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    public void doParse(String data){
        String[] dataSpilt = data.split("\\|");
        if(dataSpilt.length <= 0 ){
            return ;
        }
        Arrays.stream(dataSpilt);
    }

    @Test
    public void testSpilt(){
        String[] dataSpilt = "WER|WER1".split("\\|");
        Arrays.stream(dataSpilt).forEach(str -> System.out.println(str));
    }
}